/*
 * @Descripttion: 
 * @Author: chendingfengmail@163.com
 * @version: 
 * @Date: 2021-05-26 16:53:41
 * @LastEditors: chendingfengmail@163.com
 * @LastEditTime: 2021-11-03 15:08:37
 */
import Cookies from 'js-cookie'
const cookies = {}

let clientName = 'admin';

/**
 * @description 存储 cookie 值
 * @param {String} name cookie name
 * @param {String} value cookie value
 * @param {Object} setting cookie setting
 */
cookies.set = function (name = 'default', value = '', cookieSetting = {}) {
  const currentCookieSetting = {
    expires: 1
  }
  Object.assign(currentCookieSetting, cookieSetting)
  Cookies.set(`${clientName}-${process.env.VUE_APP_VERSION}-${name}`, value, currentCookieSetting)
}

/**
 * @description 拿到 cookie 值
 * @param {String} name cookie name
 */
cookies.get = function (name = 'default') {
  return Cookies.getJSON(`${clientName}-${process.env.VUE_APP_VERSION}-${name}`)
}

/**
 * @description 拿到 cookie 全部的值
 */
cookies.getAll = function () {
  let clientCookies = {};
  let cookies = Cookies.get();
  for (const key in cookies) {
    let keyPrefix = key.split('-')[0];
    if (keyPrefix == clientName) {
      clientCookies[key] = cookies[key]
    }
  }
  return clientCookies;
}

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
cookies.remove = function (name = 'default') {
  return Cookies.remove(`${clientName}-${process.env.VUE_APP_VERSION}-${name}`)
}

/**
 * @description 删除 cookie 全部的值
 * @param {String} name cookie name
 */
cookies.removeAll = function () {
  let keys = Object.keys(this.getAll());
  keys.forEach(function (item) {
    Cookies.remove(item)
  });
}

export default cookies
